package com.campsys.controller;

import com.campsys.core.Result;
import com.campsys.domain.TCourseLearnUnit;
import com.campsys.domain.param.ChapterAddParam;
import com.campsys.domain.param.LearnUnitUpdateParam;
import com.campsys.domain.param.SectionAddParam;
import com.campsys.mapping.LearnUnitMapping;
import com.campsys.service.ILearnUnitService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

@Api(tags = "教学管理")
@RestController
@RequestMapping("/unit")
public class LearnUnitController {

    @Autowired
    private ILearnUnitService learnUnitService;

    @ApiOperation("添加章")
    @PreAuthorize("@lcc.myAuthority('unit:add:chapter')")
    @PostMapping("/add/chapter")
    public Result addChapter(@RequestBody ChapterAddParam chapterAddParam){

        TCourseLearnUnit tCourseLearnUnit = LearnUnitMapping.INSTANCE.to(chapterAddParam);
        tCourseLearnUnit.setFatherId(0l);
        tCourseLearnUnit.setIsChpt("1");

        boolean rs = learnUnitService.save(tCourseLearnUnit);

        return rs ? Result.success() : Result.error();
    }

    @ApiOperation("添加小节")
    @PreAuthorize("@lcc.myAuthority('unit:add:section')")
    @PostMapping("/add/section")
    public Result addSection(@RequestBody SectionAddParam sectionAddParam){

        TCourseLearnUnit tCourseLearnUnit = LearnUnitMapping.INSTANCE.to(sectionAddParam);
        tCourseLearnUnit.setIsChpt("0");

        boolean rs = learnUnitService.save(tCourseLearnUnit);

        return rs ? Result.success() : Result.error();
    }

    @ApiOperation("查询课程详细章节")
    @PreAuthorize("@lcc.myAuthority('unit:list')")
    @GetMapping("/list/{courseId}")
    public Result getList(@PathVariable Long courseId){

        return Result.success(learnUnitService.list(courseId));
    }

    @ApiOperation("开放学习单元")
    @PreAuthorize("@lcc.myAuthority('unit:open')")
    @PutMapping("/open/{id}")
    public Result openUnit(@PathVariable Long id){

        TCourseLearnUnit tCourseLearnUnit = new TCourseLearnUnit();
        tCourseLearnUnit.setId(id);
        tCourseLearnUnit.setStatus(1l);

        boolean rs = learnUnitService.updateById(tCourseLearnUnit);

        return rs ? Result.success() : Result.error();
    }

    @ApiOperation("关闭学习单元")
    @PreAuthorize("@lcc.myAuthority('unit:close')")
    @PutMapping("/close/{id}")
    public Result closeUnit(@PathVariable Long id){

        TCourseLearnUnit tCourseLearnUnit = new TCourseLearnUnit();
        tCourseLearnUnit.setId(id);
        tCourseLearnUnit.setStatus(0l);

        boolean rs = learnUnitService.updateById(tCourseLearnUnit);

        return rs ? Result.success() : Result.error();
    }

    @ApiOperation("删除学习单元")
    @PreAuthorize("@lcc.myAuthority('unit:delete')")
    @PutMapping("/delete/{id}")
    public Result deleteUnit(@PathVariable Long id){

        TCourseLearnUnit tCourseLearnUnit = new TCourseLearnUnit();
        tCourseLearnUnit.setId(id);
        tCourseLearnUnit.setStatus(2l);

        boolean rs = learnUnitService.updateById(tCourseLearnUnit);

        return rs ? Result.success() : Result.error();
    }

    @ApiOperation("小节修改视频路径")
    @PreAuthorize("@lcc.myAuthority('unit:update:video')")
    @PutMapping("/update/video")
    public Result updateSectionVideo(Long id, String videoPath){

        TCourseLearnUnit tCourseLearnUnit = new TCourseLearnUnit();
        tCourseLearnUnit.setId(id);
        tCourseLearnUnit.setVideoPath(videoPath);

        boolean rs = learnUnitService.updateById(tCourseLearnUnit);

        return rs ? Result.success() : Result.error();
    }

    @ApiOperation("小节修改文档路径")
    @PreAuthorize("@lcc.myAuthority('unit:update:doc')")
    @PutMapping("/update/doc")
    public Result updateSectionDoc(Long id, String docPath){

        TCourseLearnUnit tCourseLearnUnit = new TCourseLearnUnit();
        tCourseLearnUnit.setId(id);
        tCourseLearnUnit.setDocPath(docPath);

        boolean rs = learnUnitService.updateById(tCourseLearnUnit);

        return rs ? Result.success() : Result.error();
    }

    @ApiOperation("修改学习单元")
    @PreAuthorize("@lcc.myAuthority('unit:update')")
    @PutMapping("/update")
    public Result updateUnit(@RequestBody LearnUnitUpdateParam learnUnitUpdateParam){

        boolean rs = learnUnitService.updateById(LearnUnitMapping.INSTANCE.to(learnUnitUpdateParam));

        return rs ? Result.success() : Result.error();
    }

}
